package com.xiaoxin.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.xiaoxin.model.mongo.RecommendUser;
import com.xiaoxin.model.mongo.UserLike;
import com.xiaoxin.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


/**
 * @author xiaoxiaode
 * @date 2021-09-01-14:09
 **/
@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    // 查询今日佳人
    public RecommendUser maxScore(Long toUserId) {
        // 根据userId查询,根据评分排序,获取第一条
        // 构建criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        // 构建query对象
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        // 调用mongoTemplate查询
        return mongoTemplate.findOne(query, RecommendUser.class);

    }

    // 分页查询
    public PageResult findUserList(Integer page, Integer pagesize, Long toUserId) {
        // 构建criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        // 构建query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(pagesize)
                .skip((page - 1) * pagesize);
        // 查询总数
        long count = mongoTemplate.count(query, RecommendUser.class);
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        // 构造返回值
        return new PageResult(page, pagesize, count, list);
    }

    // 查询缘分值
    public RecommendUser findScore(Long userId, Long toUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("toUserId").is(toUserId);
        Query query=Query.query(criteria);
        RecommendUser user = mongoTemplate.findOne(query, RecommendUser.class);
        if(user==null){
            user=new RecommendUser();
            user.setUserId(userId);
            user.setToUserId(toUserId);
            user.setScore(80D);
        }
        return user;
    }

    /*
     * 查询推荐列表，查询时需要排除喜欢和不喜欢的用户
     * 1、排除喜欢，不喜欢的用户
     * 2、随机展示
     * 3、指定数量
     */
    public List<RecommendUser> findRecommend(Long userId, int count) {
       Query query1=Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query1, UserLike.class);
        List<Long> ids = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        Criteria criteria=Criteria.where("toUserId").is(userId).and("userId").nin(ids);
        // 使用统计函数，随机获取推荐的用户列表
        List<RecommendUser> recommendUsers = mongoTemplate.find(Query.query(criteria), RecommendUser.class);
        TypedAggregation<RecommendUser> newAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),//指定查询条件
                Aggregation.sample(count)
        );
        AggregationResults<RecommendUser> aggregate = mongoTemplate.aggregate(newAggregation, RecommendUser.class);
        return aggregate.getMappedResults();
    }
}
